package com.crm.service.impl.productManager;

import com.crm.common.model.Enum.StatusCodeEnum;
import com.crm.dao.mapper.customMapper.productManagerCustomMapper.CustomerMemberProductCustomMapper;
import com.crm.dao.mapper.customMapper.productManagerCustomMapper.CustomerProductCustomMapper;
import com.crm.dao.mapper.productManager.CustomerMemberProductMapper;
import com.crm.dao.mapper.sys.FileUploadMapper;
import com.crm.model.entity.productManager.CustomerMemberProduct;
import com.crm.model.entity.productManager.CustomerMemberProductExample;
import com.crm.model.entity.sys.FileUpload;
import com.crm.model.qo.productManager.CustomerMemberProductEditQO;
import com.crm.model.vo.productManager.CourseSubVO;
import com.crm.model.vo.productManager.CustomerProductDetailVO;
import com.crm.model.vo.productManager.ProductVO;
import com.crm.service.productManager.CustomerMemberProductDbService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author yincuimei
 * @Date 2021/4/12 15:45
 * 产品管理--会员产品
 */
@Service
public class CustomerMemberProductDbServiceImpl implements CustomerMemberProductDbService {
    @Autowired
    private CustomerMemberProductMapper memberProductMapper;
    @Autowired
    private CustomerProductCustomMapper customerProductCustomMapper;
    @Autowired
    private FileUploadMapper fileUploadMapper;
    @Autowired
    private CustomerMemberProductCustomMapper memberProductCustomMapper;

    /**
     * 产品管理--会员产品列表
     * @param productStatus  ---产品停、启用状态
     * @param memberCardName ---会员卡名称
     * @param sysType        ---系统类型(用户-user/管理-management)
     * @param order          ---排序
     * @author yincuimei
     */
    @Override
    public List<CustomerMemberProduct> selectCustomerProductList(String productStatus, String memberCardName, String sysType,String order) {
        CustomerMemberProductExample memberProductExample = new CustomerMemberProductExample();
        CustomerMemberProductExample.Criteria criteria = memberProductExample.createCriteria();
        if(!StringUtils.isBlank(sysType)){
            if("user".equals(sysType)){//用户系统(只查启用状态的会员产品)
                criteria.andProductStatusEqualTo("0");
            }
        }
        if (!StringUtils.isBlank(productStatus)) {//管理系统的查询条件
            criteria.andProductStatusEqualTo(productStatus);//产品停、启用状态
        }
        if (!StringUtils.isBlank(memberCardName)) {//会员卡名称
            criteria.andMemberCardNameLike("%" + memberCardName + "%");
        }
        criteria.andDeleteStatusEqualTo((short) StatusCodeEnum.NORMAL_CODE.getState());//删除状态
        if (!StringUtils.isBlank(order)) {
            memberProductExample.setOrderByClause(order);
        }
        return memberProductMapper.selectByExample(memberProductExample);
    }

    /**
     * 产品管理--新增会员产品
     *
     * @param customerMemberProduct ---会员产品信息
     * @return
     * @author yincuimei
     */
    @Override
    public Boolean insertCustomerMemberProduct(CustomerMemberProduct customerMemberProduct) {
        return memberProductMapper.insertSelective(customerMemberProduct) > 0;
    }

    /**
     * 新增会员产品--查询会员卡名称是否存在
     * @param memberCodeName ---会员卡名称
     * @param deleteStatus   ---删除状态:0-正常;1-已删除
     * @author yincuimei
     */
    @Override
    public Boolean isExistMemberCardName(String memberCodeName, short deleteStatus) {
        CustomerMemberProductExample memberProductExample = new CustomerMemberProductExample();
        CustomerMemberProductExample.Criteria criteria = memberProductExample.createCriteria();
        criteria.andMemberCardNameEqualTo(memberCodeName);//会员卡名称
        criteria.andDeleteStatusEqualTo(deleteStatus);//删除状态
        List<CustomerMemberProduct> memberProductList = memberProductMapper.selectByExample(memberProductExample);
        return memberProductList != null && memberProductList.size() > 0;
    }

    /**
     * 新增会员产品--查询会员产品编码是否存在
     *
     * @param memberProductCode ---会员产品编码
     * @param deleteStatus      ---删除状态:0-正常;1-已删除
     * @return
     * @author yincuimei
     */
    @Override
    public Boolean isExistMemberProductCode(String memberProductCode, short deleteStatus) {
        CustomerMemberProductExample memberProductExample = new CustomerMemberProductExample();
        CustomerMemberProductExample.Criteria criteria = memberProductExample.createCriteria();
        criteria.andMemberProductCodeEqualTo(memberProductCode);//会员产品编码
        criteria.andDeleteStatusEqualTo(deleteStatus);//删除状态
        List<CustomerMemberProduct> memberProductList = memberProductMapper.selectByExample(memberProductExample);
        return memberProductList != null && memberProductList.size() > 0;
    }

    /**
     * 会员产品列表--会员卡产品停、启用操作
     *
     * @param customerMemberProduct ---会员产品信息
     * @return
     * @author yincuimei
     */
    @Override
    public Boolean updateProductStatusById(CustomerMemberProduct customerMemberProduct) {
        return memberProductMapper.updateByPrimaryKeySelective(customerMemberProduct) > 0;
    }

    /**
     * 根据产品id查询产品详情
     *
     * @param memberProductId
     * @return
     */
    @Override
    public List<CustomerProductDetailVO> queryCustomerProductDetailInfo(String memberProductId) {
        List<CustomerProductDetailVO> resultList = new ArrayList<>();
        CustomerProductDetailVO detail = new CustomerProductDetailVO();
        List<CourseSubVO> customerProductDetailList = customerProductCustomMapper.queryCustomerProductDetailInfo(memberProductId);
        if (customerProductDetailList != null && customerProductDetailList.size() > 0) {
            detail.setCourseSub(customerProductDetailList);
        }
        //给返回结果的 卡片名称、销售价格赋值
        CustomerMemberProduct customerMemberProduct = memberProductMapper.selectByPrimaryKey(memberProductId);
        //查看图片url
        FileUpload fileUpload = fileUploadMapper.selectByPrimaryKey(customerMemberProduct.getMemberCardImg());
        detail.setMemberCardName(customerMemberProduct.getMemberCardName());
        detail.setSellingCardPrice(customerMemberProduct.getSellingCardPrice());
        detail.setMemberProductCode(customerMemberProduct.getMemberProductCode());
        detail.setProductMembershipLevel(customerMemberProduct.getProductMembershipLevel());
        detail.setProductPeriodValidityStart(customerMemberProduct.getProductPeriodValidityStart());
        detail.setProductPeriodValidityEnd(customerMemberProduct.getProductPeriodValidityEnd());
        if (fileUpload != null) {
            detail.setMemberCardImg(fileUpload.getAccessUrl());
        }
        detail.setServiceNumber(customerMemberProduct.getServiceNumber());
        detail.setPerformanceConversionRatio(customerMemberProduct.getPerformanceConversionRatio());
        detail.setWhetherIncludedAchievement(customerMemberProduct.getWhetherIncludedAchievement());
        detail.setWhetherRenew(customerMemberProduct.getWhetherRenew());//是否可续费产品(7-30加的)
        resultList.add(detail);
        return resultList;
    }


    /**
     * 根据产品id修改产品销售价格
     *
     * @param
     * @param
     * @return
     */
    @Override
    public boolean updateCardPrice(CustomerMemberProductEditQO memberEditQO) {
        CustomerMemberProduct customerMemberProduct = memberProductMapper.selectByPrimaryKey(memberEditQO.getMemberProductId());
        if (null != memberEditQO.getSellingCardPrice()) {
            customerMemberProduct.setSellingCardPrice(memberEditQO.getSellingCardPrice());
        }
        if (StringUtils.isNotBlank(memberEditQO.getMemberCardImg())) {
            customerMemberProduct.setMemberCardImg(memberEditQO.getMemberCardImg());
        }
        if (StringUtils.isNotBlank(memberEditQO.getMemberCardName())) {
            customerMemberProduct.setMemberCardName(memberEditQO.getMemberCardName());
        }
        if (null != memberEditQO.getServiceNumber()) {
            customerMemberProduct.setServiceNumber(memberEditQO.getServiceNumber());
        }
        if (null != memberEditQO.getMembershipLevel()) {
            customerMemberProduct.setProductMembershipLevel(memberEditQO.getMembershipLevel());
        }
        if (StringUtils.isNotBlank(memberEditQO.getWhetherIncludedAchievement())) {
            customerMemberProduct.setWhetherIncludedAchievement(memberEditQO.getWhetherIncludedAchievement());
        }
        if (null != memberEditQO.getPerformanceConversionRatio()) {
            customerMemberProduct.setPerformanceConversionRatio(memberEditQO.getPerformanceConversionRatio());
        }
        if (null != memberEditQO.getProductPeriodValidityStart()) {
            customerMemberProduct.setProductPeriodValidityStart(memberEditQO.getProductPeriodValidityStart());
        }
        if (null != memberEditQO.getProductPeriodValidityEnd()) {
            customerMemberProduct.setProductPeriodValidityEnd(memberEditQO.getProductPeriodValidityEnd());
        }
        if(!StringUtils.isBlank(memberEditQO.getWhetherRenew())){//是否可续费产品
            customerMemberProduct.setWhetherRenew(memberEditQO.getWhetherRenew());
        }
        int updateCount = memberProductMapper.updateByPrimaryKeySelective(customerMemberProduct);
        return updateCount > 0;

    }

    /**
     * 新增合同--会员产品列表
     *
     * @param productName ---产品名称
     * @return
     * @author yincuimei
     */
    @Override
    public List<CustomerMemberProduct> selectContractMemberProductList(String productName,String whetherRenew,String upgrade) {
        CustomerMemberProductExample memberProductExample = new CustomerMemberProductExample();
        CustomerMemberProductExample.Criteria criteria = memberProductExample.createCriteria();
        criteria.andProductStatusEqualTo("0");//产品停、启用状态
        if (!StringUtils.isBlank(productName)) {
            criteria.andMemberCardNameLike("%" + productName + "%");//产品名称
        }
        criteria.andDeleteStatusEqualTo((short) StatusCodeEnum.NORMAL_CODE.getState());//删除状态
        //判断是否是续费合同（续费合同需要加产品标识）
        if(!StringUtils.isBlank(whetherRenew)){
            criteria.andWhetherRenewEqualTo(whetherRenew);
        }
        //是否是补款升级查询会员产品
        if(!StringUtils.isBlank(upgrade)){
            //补款升级不需要这个产品
            criteria.andMemberCardNameNotEqualTo("会员到款增额");
        }
        memberProductExample.setOrderByClause("selling_card_price asc");
        return memberProductMapper.selectByExample(memberProductExample);
    }

    /***
     * @Description:根据会员产品id查询会员产品信息
     * @Param:memberProductId 会员产品id
     * @Author: luojie
     * @Date: 2021/5/8 13:55
     */
    @Override
    public CustomerMemberProduct selectMemberProductById(String memberProductId) {
        CustomerMemberProduct customerMemberProduct = memberProductMapper.selectByPrimaryKey(memberProductId);
        return customerMemberProduct;
    }

    /**
     * 根据会员卡名称或者会员产品编码查询会员产品信息
     *
     * @param memberCodeName    会员卡名称
     * @param memberProductCode 会员产品编码
     * @return {@link {com.crm.model.entity.productManager.CustomerMemberProduct}}
     * @Author pym
     * @Date 2021/6/3 15:53
     */
    @Override
    public CustomerMemberProduct selectMemberProductByNameOrCode(String memberCodeName, String memberProductCode) {
        CustomerMemberProductExample memberProductExample = new CustomerMemberProductExample();
        CustomerMemberProductExample.Criteria criteria = memberProductExample.createCriteria();
        criteria.andMemberCardNameEqualTo(memberCodeName);//会员卡名称
        criteria.andDeleteStatusEqualTo(Short.parseShort(String.valueOf(StatusCodeEnum.NORMAL_CODE.getState())));//删除状态
        if (StringUtils.isNotBlank(memberProductCode)) {
            CustomerMemberProductExample.Criteria criteria2 = memberProductExample.createCriteria();
            criteria2.andMemberProductCodeEqualTo(memberProductCode);//会员产品编码
            criteria2.andDeleteStatusEqualTo(Short.parseShort(String.valueOf(StatusCodeEnum.NORMAL_CODE.getState())));//删除状态
            memberProductExample.or(criteria2);
        }
        List<CustomerMemberProduct> memberProductList = memberProductMapper.selectByExample(memberProductExample);
        if (memberProductList != null && memberProductList.size() > 0) {
            return memberProductList.get(0);
        }
        return null;
    }

    /**
     * 新增会员产品--查询会员产品编码是否存在
     *
     * @param memberProductCode ---会员产品编码
     * @param deleteStatus      ---删除状态:0-正常;1-已删除
     * @return
     * @author baijian
     */
    @Override
    public CustomerMemberProduct queryMemberProductCode(String memberProductCode, short deleteStatus) {
        CustomerMemberProductExample memberProductExample = new CustomerMemberProductExample();
        CustomerMemberProductExample.Criteria criteria = memberProductExample.createCriteria();
        criteria.andMemberProductCodeEqualTo(memberProductCode);//会员产品编码
        criteria.andDeleteStatusEqualTo(deleteStatus);//删除状态
        List<CustomerMemberProduct> memberProductList = memberProductMapper.selectByExample(memberProductExample);
        if (memberProductList != null && memberProductList.size() > 0) {
            return memberProductList.get(0);
        } else {
            return new CustomerMemberProduct();
        }
    }

    /**
     * 新增商机--会员产品列表
     *
     * @return
     * @author baijian
     */
    @Override
    public List<CustomerMemberProduct> selectSjCustomerMemberProductVOList(String productStatus, String memberCardName, String order, String tag) {
        CustomerMemberProductExample memberProductExample = new CustomerMemberProductExample();
        CustomerMemberProductExample.Criteria criteria = memberProductExample.createCriteria();
        criteria.andProductStatusEqualTo("0");//产品停、启用状态
        if (!StringUtils.isBlank(memberCardName)) {
            criteria.andMemberCardNameLike("%" + memberCardName + "%");
        }
        criteria.andDeleteStatusEqualTo((short) StatusCodeEnum.NORMAL_CODE.getState());//删除状态
        if (!StringUtils.isBlank(order)) {
            memberProductExample.setOrderByClause(order);
        }
        return memberProductMapper.selectByExample(memberProductExample);
    }

    /**
     * 根据产品类型和id查询产品业绩换算比例
     * @param productCategories
     * @param productId
     * @return
     */
    @Override
    public BigDecimal selectProductPerformanceRatio(String productCategories, String productId) {
        return customerProductCustomMapper.selectProductPerformanceRatio(productCategories,productId);
    }

    /**
     * 根据会员产品id列表查询会员产品名称
     * @param memberProductIdList 会员产品id列表
     * @Author renshaorong
     * @Date 2021/11/23
     */
    @Override
    public List<Map<String,String>> selectMemberProductByIdList(List<Map<String,String>> memberProductIdList) {
        return customerProductCustomMapper.selectMemberProductByIdList(memberProductIdList);
    }

    /**
     * 查询所有【启用状态】的会员卡名称
     * @author yincuimei
     * @return
     */
    @Override
    public List<String> selectMemberCardNameByProductStatus() {
        return memberProductCustomMapper.selectMemberCardNameByProductStatus();
    }

    /**
     * 查询会员上架产品
     * @Author pym
     * @Date  2021/12/29 16:55
     * @param
     * @return {@link {java.util.List<com.crm.model.vo.productManager.ProductVO>}}
     */
    @Override
    public List<ProductVO> selectMemberShelfProductList() {
        return memberProductCustomMapper.selectMemberShelfProductList();
    }
}
